/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import libsvm.svm;
import libsvm.svm_model;
import svm_oao.SVMUsage;
import data_access.DataAccess;
import data_access.ResultObj;

/**
 * 
 * @author Anh
 */
public class Screen extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static String modelFileName = "";
	static String outputFileName = "";
	String currentPath = "";

	/**
	 * Creates new form Screen
	 */
	public Screen() {
		initComponents();
	}

	private void runPredictOAO(final int type, final ResultObj obj) {
		try {
			final File fileTest = new File(tfTestFile.getText());
			final File fileModel = new File(tfModelFile.getText());
			if (fileTest.isFile() && fileModel.isFile()) {
				progressOAO.setValue(0);
				final int predict_probability = 1;
				final svm_model model = svm.svm_load_model(tfModelFile.getText());
				if (model == null) {
					progressOAO.setValue(0);
					JOptionPane.showMessageDialog(null, "Can't open Model file!");
					return;
				}

				final Thread t1 = new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							outputFileName = SVMUsage.predictData(fileTest, model, predict_probability, type, obj);
						} catch (Exception ex) {
							progressOAO.setValue(0);
							JOptionPane.showMessageDialog(null, ex.getMessage());
							ex.printStackTrace();
						}
					}
				});
				t1.start();
				Thread t2 = new Thread(new Runnable() {
					@Override
					public void run() {
						while (progressOAO.getValue() < 100) {

							if (progressOAO.getValue() < 99 && t1.isAlive()) {
								progressOAO.setValue(progressOAO.getValue() + 1);
								try {
									Thread.sleep(20);
								} catch (InterruptedException ex) {
									JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
											JOptionPane.ERROR_MESSAGE);
								}
							} else if (progressOAO.getValue() == 99 || !t1.isAlive()) {
								try {
									t1.join();
								} catch (InterruptedException ex) {
									JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
											JOptionPane.ERROR_MESSAGE);
								}
								if (!"".equals(outputFileName)) {
									String result = SVMUsage.getAccturacy();
									if ("".equals(result) || "error".equals(result)) {
										tfAccOAO.setText("");
										tfTimeOAO.setText("");
										progressOAO.setValue(0);
										JOptionPane.showMessageDialog(null, "This is not Test file format!");
										break;
									} else {
										progressOAO.setValue(100);
										tfAccOAO.setText(result);
										tfTimeOAO.setText("" + SVMUsage.getTime());
										obj.setDataName(fileTest.getName());
										obj.setTimeOAO(Integer.parseInt("" + SVMUsage.getTime()));
										int type = 2;
										runPredictDDAG(type, obj);
										break;

									}
								} else {
									tfAccOAO.setText("");
									tfTimeOAO.setText("");
									progressOAO.setValue(0);
									JOptionPane.showMessageDialog(null, "This is not Test file format!");
									break;
								}
							}
						}
					}
				});
				t2.start();
			} else {
				JOptionPane.showMessageDialog(null, "Pls give Model file and Test file!");
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e);
			System.err.print(e);
		}
	}

	private void runPredictDDAG(final int type, final ResultObj obj) {
		try {
			final File fileTest = new File(tfTestFile.getText());
			final File fileModel = new File(tfModelFile.getText());
			if (fileTest.isFile() && fileModel.isFile()) {
				progressDDAG.setValue(0);
				final int predict_probability = 1;
				final svm_model model = svm.svm_load_model(tfModelFile.getText());
				if (model == null) {
					progressDDAG.setValue(0);
					JOptionPane.showMessageDialog(null, "Can't open Model file!");
					return;
				}

				final Thread t1 = new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							outputFileName = SVMUsage.predictData(fileTest, model, predict_probability, type, obj);
						} catch (Exception ex) {
							progressDDAG.setValue(0);
							JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
				t1.start();
				Thread t2 = new Thread(new Runnable() {
					@Override
					public void run() {
						while (progressDDAG.getValue() < 100) {

							if (progressDDAG.getValue() < 99 && t1.isAlive()) {
								progressDDAG.setValue(progressDDAG.getValue() + 1);
								try {
									Thread.sleep(20);
								} catch (InterruptedException ex) {
									JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
											JOptionPane.ERROR_MESSAGE);
								}
							} else if (progressDDAG.getValue() == 99 || !t1.isAlive()) {
								try {
									t1.join();
								} catch (InterruptedException ex) {
									JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
											JOptionPane.ERROR_MESSAGE);
								}
								if (!"".equals(outputFileName)) {
									String result = SVMUsage.getAccturacy();
									if ("".equals(result) || "error".equals(result)) {
										tfAccDDAG.setText("");
										tfTimeDDAG.setText("");
										progressDDAG.setValue(0);
										JOptionPane.showMessageDialog(null, "This is not Test file format!");
										break;
									} else {
										progressDDAG.setValue(100);
										tfAccDDAG.setText(result);
										tfTimeDDAG.setText("" + SVMUsage.getTime());
										obj.setTimeDDAG(Integer.parseInt("" + SVMUsage.getTime()));
										int type = 3;
										runPredictADAG(type, obj);

										break;
									}
								} else {
									tfAccDDAG.setText("");
									tfTimeDDAG.setText("");
									progressDDAG.setValue(0);
									JOptionPane.showMessageDialog(null, "This is not Test file format!");
									break;
								}
							}
						}
					}
				});
				t2.start();
			} else {
				JOptionPane.showMessageDialog(null, "Pls give Model file and Test file!");
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e);
			System.err.print(e);
		}
	}

	private void runPredictADAG(final int type, final ResultObj obj) {
		try {
			final File fileTest = new File(tfTestFile.getText());
			final File fileModel = new File(tfModelFile.getText());
			if (fileTest.isFile() && fileModel.isFile()) {
				progressADAG.setValue(0);
				final int predict_probability = 1;
				final svm_model model = svm.svm_load_model(tfModelFile.getText());
				if (model == null) {
					progressADAG.setValue(0);
					JOptionPane.showMessageDialog(null, "Can't open Model file!");
					return;
				}

				final Thread t1 = new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							outputFileName = SVMUsage.predictData(fileTest, model, predict_probability, type, obj);
						} catch (Exception ex) {
							progressADAG.setValue(0);
							JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
				t1.start();
				Thread t2 = new Thread(new Runnable() {
					@Override
					public void run() {
						while (progressADAG.getValue() < 100) {

							if (progressADAG.getValue() < 99 && t1.isAlive()) {
								progressADAG.setValue(progressADAG.getValue() + 1);
								try {
									Thread.sleep(20);
								} catch (InterruptedException ex) {
									JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
											JOptionPane.ERROR_MESSAGE);
								}
							} else if (progressADAG.getValue() == 99 || !t1.isAlive()) {
								try {
									t1.join();
								} catch (InterruptedException ex) {
									JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
											JOptionPane.ERROR_MESSAGE);
								}
								if (!"".equals(outputFileName)) {
									String result = SVMUsage.getAccturacy();
									if ("".equals(result) || "error".equals(result)) {
										tfAccADAG.setText("");
										tfTimeADAG.setText("");
										progressADAG.setValue(0);
										JOptionPane.showMessageDialog(null, "This is not Test file format!");
										break;
									} else {
										progressADAG.setValue(100);
										int success = JOptionPane.showConfirmDialog(null, "Testing successfull!",
												"Success", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
										if (success == JOptionPane.CLOSED_OPTION || success == JOptionPane.OK_OPTION) {
											progressOAO.setValue(0);
											progressDDAG.setValue(0);
											progressADAG.setValue(0);
											tfAccADAG.setText(result);
											tfTimeADAG.setText("" + SVMUsage.getTime());
											obj.setTimeADAG(Integer.parseInt("" + SVMUsage.getTime()));
											lbDataName.setText("Data Name: " + obj.getDataName());
											lbNumberClass.setText("Number of Class: " + obj.getClassNumber());
											lbTestingSize.setText("Testing Size: " + obj.getTestingSize());
                                                                                        lbDataName.setMaximumSize(lbDataName.getSize());
											try {
												DataAccess da = new DataAccess();
												da.insertResult(obj);
												da.closeConnect();
											} catch (Exception e) {
												JOptionPane.showMessageDialog(null, e.getMessage(), "Error",
														JOptionPane.ERROR_MESSAGE);
											}
											break;
										}
									}
								} else {
									tfAccADAG.setText("");
									tfTimeADAG.setText("");
									progressADAG.setValue(0);
									JOptionPane.showMessageDialog(null, "This is not Test file format!");
									break;
								}
							}
						}
					}
				});
				t2.start();
			} else {
				JOptionPane.showMessageDialog(null, "Pls give Model file and Test file!");
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e);
			System.err.print(e);
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnLoadTrain = new javax.swing.JButton();
        tfTrainFile = new javax.swing.JTextField();
        btnTrain = new javax.swing.JButton();
        progressTrain = new javax.swing.JProgressBar();
        tfTimeTrain = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnLoadModel = new javax.swing.JButton();
        tfModelFile = new javax.swing.JTextField();
        btnLoadTest = new javax.swing.JButton();
        tfTestFile = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        btnStart = new javax.swing.JButton();
        tfTimeDDAG = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tfAccDDAG = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfTimeOAO = new javax.swing.JTextField();
        tfAccOAO = new javax.swing.JTextField();
        btnHistory = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        tfAccADAG = new javax.swing.JTextField();
        tfTimeADAG = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        progressOAO = new javax.swing.JProgressBar();
        progressDDAG = new javax.swing.JProgressBar();
        progressADAG = new javax.swing.JProgressBar();
        jPanel4 = new javax.swing.JPanel();
        lbDataName = new javax.swing.JLabel();
        lbTestingSize = new javax.swing.JLabel();
        lbNumberClass = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Data Classification");
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/Resources/java.png")).getImage());
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 204));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Train Data"));

        btnLoadTrain.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/load.png"))); // NOI18N
        btnLoadTrain.setText("Load Train File");
        btnLoadTrain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadTrainActionPerformed(evt);
            }
        });

        tfTrainFile.setEditable(false);

        btnTrain.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/train.png"))); // NOI18N
        btnTrain.setText("Train");
        btnTrain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrainActionPerformed(evt);
            }
        });

        progressTrain.setStringPainted(true);

        tfTimeTrain.setEditable(false);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Time");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(tfTimeTrain, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnTrain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnLoadTrain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(progressTrain, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                            .addComponent(tfTrainFile))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLoadTrain)
                    .addComponent(tfTrainFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTrain)
                    .addComponent(progressTrain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfTimeTrain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 204));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Prepare Data"));

        btnLoadModel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/load.png"))); // NOI18N
        btnLoadModel.setText("Load Model");
        btnLoadModel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadModelActionPerformed(evt);
            }
        });

        tfModelFile.setEditable(false);

        btnLoadTest.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/load.png"))); // NOI18N
        btnLoadTest.setText("Load Test File");
        btnLoadTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadTestActionPerformed(evt);
            }
        });

        tfTestFile.setEditable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnLoadModel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnLoadTest, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tfModelFile)
                    .addComponent(tfTestFile, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLoadModel)
                    .addComponent(tfModelFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLoadTest)
                    .addComponent(tfTestFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        jPanel3.setBackground(new java.awt.Color(255, 255, 204));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Predict Data"));

        btnStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/test.png"))); // NOI18N
        btnStart.setText("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        tfTimeDDAG.setEditable(false);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Time");

        tfAccDDAG.setEditable(false);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Time");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("Accuracy");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Accuracy");

        tfTimeOAO.setEditable(false);

        tfAccOAO.setEditable(false);

        btnHistory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/history.png"))); // NOI18N
        btnHistory.setText("History");
        btnHistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHistoryActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setText("Accuracy");

        tfAccADAG.setEditable(false);

        tfTimeADAG.setEditable(false);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Time");

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("SVM_DDAG");

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("SVM_OAO");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("SVM_ADAG");

        progressOAO.setStringPainted(true);

        progressDDAG.setStringPainted(true);

        progressADAG.setStringPainted(true);

        jPanel4.setBackground(new java.awt.Color(255, 255, 204));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Infomation", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 0, 10))); // NOI18N

        lbDataName.setText("Data Name: ");

        lbTestingSize.setText("Testing Size: ");

        lbNumberClass.setText("Number of Class: ");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbTestingSize)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(lbDataName, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lbNumberClass, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbDataName)
                    .addComponent(lbNumberClass))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbTestingSize)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfAccOAO, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfTimeOAO, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(progressOAO, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfAccDDAG, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfTimeDDAG, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(progressDDAG, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(93, 93, 93)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(progressADAG, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfAccADAG, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfTimeADAG, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(69, 69, 69))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnHistory)
                    .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(82, 82, 82)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(147, 147, 147))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnStart)
                        .addGap(18, 18, 18)
                        .addComponent(btnHistory))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(progressOAO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(progressDDAG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(progressADAG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfAccOAO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfAccDDAG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfAccADAG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel7)
                    .addComponent(tfTimeOAO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(tfTimeDDAG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(tfTimeADAG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(30, 30, 30)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void btnLoadTrainActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnLoadTrainActionPerformed
		JFileChooser chooser = new JFileChooser();
		if (!"".equals(currentPath)) {
			chooser.setCurrentDirectory(new File(currentPath));
		}
		int choose = chooser.showOpenDialog(this);
		if (choose == JFileChooser.APPROVE_OPTION) {
			try {
				final File file = chooser.getSelectedFile();
				if (file != null) {
					String fileName = file.getAbsolutePath();
					currentPath = fileName;
					tfTrainFile.setText(fileName);
				}
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, e.getMessage());
			}
		}
	}// GEN-LAST:event_btnLoadTrainActionPerformed

	private void btnTrainActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnTrainActionPerformed
		try {
			final File file = new File(tfTrainFile.getText());
			if (file.isFile()) {

				final Thread t1 = new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							modelFileName = SVMUsage.trainData(file);
						} catch (Exception ex) {
							ex.printStackTrace();
						}
					}
				});
				t1.start();
				Thread t2 = new Thread(new Runnable() {
					@Override
					public void run() {
						while (progressTrain.getValue() < 100) {

							if (progressTrain.getValue() < 99 && t1.isAlive()) {
								progressTrain.setValue(progressTrain.getValue() + 1);
								try {
									Thread.sleep(20);
								} catch (InterruptedException ex) {
									ex.printStackTrace();
								}
							} else if (progressTrain.getValue() == 99 || !t1.isAlive()) {
								try {
									t1.join();
								} catch (InterruptedException ex) {
									ex.printStackTrace();
								}
								if (!"".equals(modelFileName)) {
									progressTrain.setValue(100);
									int success = JOptionPane.showConfirmDialog(null, "Training successfull!",
											"Success", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
									if (success == JOptionPane.CLOSED_OPTION || success == JOptionPane.OK_OPTION) {
										progressTrain.setValue(0);
										tfModelFile.setText(modelFileName);
										tfTimeTrain.setText("" + SVMUsage.getTime());
										break;
									}
								} else {
									progressTrain.setValue(0);
									JOptionPane.showMessageDialog(null, "This is not file format!");
									tfModelFile.setText("");
									tfTimeTrain.setText("");
									break;
								}
							}
						}
					}
				});
				t2.start();
			} else {
				JOptionPane.showMessageDialog(null, "Pls choose file train first!");
			}
		} catch (Exception e) {
			if (SVMUsage.getError_message() != null) {
				JOptionPane.showMessageDialog(null, SVMUsage.getError_message());
				System.err.print(e);
			} else {
				JOptionPane.showMessageDialog(null, "This is not file format!");
				System.err.print(e);
			}
		}
	}// GEN-LAST:event_btnTrainActionPerformed

	private void btnLoadModelActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnLoadModelActionPerformed
		JFileChooser chooser = new JFileChooser();
		if (!"".equals(currentPath)) {
			chooser.setCurrentDirectory(new File(currentPath));
		}
		int choose = chooser.showOpenDialog(this);
		if (choose == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();
			if (file != null) {
				tfModelFile.setText(file.getAbsolutePath());
				currentPath = file.getAbsolutePath();
			}
		}
	}// GEN-LAST:event_btnLoadModelActionPerformed

	private void btnLoadTestActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnLoadTestActionPerformed
		JFileChooser chooser = new JFileChooser();
		if (!"".equals(currentPath)) {
			chooser.setCurrentDirectory(new File(currentPath));
		}
		int choose = chooser.showOpenDialog(this);
		if (choose == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();
			if (file != null) {
				tfTestFile.setText(file.getAbsolutePath());
				currentPath = file.getAbsolutePath();
			}
		}
	}// GEN-LAST:event_btnLoadTestActionPerformed

	private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnStartActionPerformed
		int type = 1;
		ResultObj obj = new ResultObj();
		runPredictOAO(type, obj);
	}// GEN-LAST:event_btnStartActionPerformed

	private void btnHistoryActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnHistoryActionPerformed
		Chart chart = new Chart(this);
		chart.setVisible(true);
		this.setEnabled(false);

	}// GEN-LAST:event_btnHistoryActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(Screen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(Screen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(Screen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(Screen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Screen().setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHistory;
    private javax.swing.JButton btnLoadModel;
    private javax.swing.JButton btnLoadTest;
    private javax.swing.JButton btnLoadTrain;
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnTrain;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel lbDataName;
    private javax.swing.JLabel lbNumberClass;
    private javax.swing.JLabel lbTestingSize;
    private javax.swing.JProgressBar progressADAG;
    private javax.swing.JProgressBar progressDDAG;
    private javax.swing.JProgressBar progressOAO;
    private javax.swing.JProgressBar progressTrain;
    private javax.swing.JTextField tfAccADAG;
    private javax.swing.JTextField tfAccDDAG;
    private javax.swing.JTextField tfAccOAO;
    private javax.swing.JTextField tfModelFile;
    private javax.swing.JTextField tfTestFile;
    private javax.swing.JTextField tfTimeADAG;
    private javax.swing.JTextField tfTimeDDAG;
    private javax.swing.JTextField tfTimeOAO;
    private javax.swing.JTextField tfTimeTrain;
    private javax.swing.JTextField tfTrainFile;
    // End of variables declaration//GEN-END:variables
}
